<?php
function createDir(){
	$dir = 'photos';

	// create new directory with 777 permissions if it does not exist yet
	// owner will be the user/group the PHP script is run under
	if ( !file_exists($dir) ) {
	mkdir ($dir, 0777);
	}
}


function savePhoto($remoteImage) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    curl_close($ch);
	$basename = basename($remoteImage);
	if ($fileContents != "")
    {
		$newImg = imagecreatefromstring($fileContents);
		return imagepng($newImg, "./photos/{$basename}",0);
	}
}
#be sure to chmod your destination dir - in my case 'photos' =]
createDir();
if (isset($_POST['url']))
	SavePhoto($_POST['url']);

?>